/*
	It started to be written by Roosevelt de Lima Sardinha, in April, 3rd, 2010.
	Roomenus is a javascript component for creating and managing menus in a web page.
*/
function roomenus(id, properties)
{
	var rmenus = this;

	// Checking if id is valid.
	if (id == null)
	{
		document.write("roomenus: id cannot be null");
	}
	else
	{
		this.id = id;
	}
	
	// CSS Configuration
	this.mainMenuCSS = properties.mainMenuCSS ? properties.mainMenuCSS : 'default-main-menu';
	this.subMenuCSS = properties.subMenuCSS ? properties.subMenuCSS : 'default-sub-menu';
	this.width = properties.width ? properties.width : 800;
	this.menuPrefix = properties.menuPrefix ? properties.menuPrefix : 'menu-';
	this.completeMenu = properties.completeMenu ? properties.completeMenu : true;
	this.menuCompletionCSS = properties.menuCompletionCSS ? properties.menuCompletionCSS : 'default-menu-completion';
	
	this.container = document.createElement('div');
	this.container.className = this.mainMenuCSS;
	this.ul = document.createElement('ul');
	this.ul.className = this.mainMenuCSS;
	this.container.appendChild(this.ul);	
	this.menus = new Array;
	
	if (this.completeMenu)
	{
		this.menuCompletion = document.createElement('div');
		this.menuCompletion.className = this.menuCompletionCSS;
		this.container.appendChild(this.menuCompletion)
	}
		
	this.appendTo = function (parentId)
	{
		// Appends this component to another container. Probably a div.
		parent = document.getElementById(parentId);
		parent.appendChild(this.container);
		this.onAppendTo(this.container);
	}
	
	var counter = 0;
	this.add = function(text, position)
	{
		// Adds a new first level menu.
		var myMenu = new menu(this.menuPrefix + counter, {
			text: text,
			menuCSS: this.subMenuCSS,
			display: 'block'
		});	
		if (position == null)
		{
			position = this.menus.length;
		}

		if (position == this.menus.length)
		{
			this.ul.appendChild(myMenu.li);
			this.menus.push(myMenu);
		}			
		else
		{
			var nextObj = this.menus[position];	
			this.ul.insertBefore(myMenu.li, nextObj.li);
			this.menus.splice(position, 0, myMenu);
		}
		counter++;
		
		myMenu.li.className = this.mainMenuCSS;
		myMenu.li.onmouseover = function(){
			var len = myMenu.menus.length;
			for(var i = 0; i < len; i++)
			{
				myMenu.menus[i].li.style.display = 'block';	
			}
		}
		
		myMenu.li.onmouseout = function(){
			var len = myMenu.menus.length;
			for(var i = 0; i < len; i++)
			{
				myMenu.menus[i].li.style.display = 'none';	
			}
		}
		
		return myMenu.id;
	}
	
	this.remove = function(position)
	{
		// Removes a first level menu.
		var removingMenu = this.menus[position]; 
		this.ul.removeChild(removingMenu.li);
		this.menus.splice(position, 1);
		removingMenu = null;
	}
	
	this.setWidth = function(width){
		// Set container width.
		this.container.style.width = width + 'px';
		this.width = width;
	}
	this.setWidth(this.width);
	
	this.onAppendTo = function(object){};
}